<template>
  <div class="hm-register">
    <hm-header>面经注册</hm-header>
    <van-form validate-first @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[
          {
            pattern: /^\w{5,}$/,
            message: '用户名不得小于5位字符',
          },
          {
            required: true,
            message: '请输入用户名',
          },
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[
          {
            pattern: /^\w{6,}$/,
            message: '密码不得小于6位字符',
          },
          {
            required: true,
            message: '请输入密码',
          },
        ]"
      />
      <div style="margin: 16px">
        <van-button color="#07c160" block type="info" native-type="submit"
          >注册</van-button
        >
      </div>
    </van-form>
    <div class="hm-login-footer">
      <router-link to="/login">有账号,去登录</router-link>
    </div>
  </div>
</template>

<script>
import { registerApi } from "@/api/user";
export default {
  name: "HmRegister",
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    async onSubmit(values) {
      //注册中
      this.$toast.loading("注册中");
      await registerApi(values);
      this.$toast.success("注册成功");
      this.$router.push("/login");
    },
  },
};
</script>

<style>
.hm-login-footer {
  text-align: right;
  margin-top: 10px;
  padding-right: 20px;
}
.hm-login-footer a {
  color: #069;
  font-size: 3.2vw;
}
</style>
